<?php
class WebsitesController extends AppController
{
	public function index()
	{
		$this->set('websites', $this->Website->find('all'));
	}
	
	public function add()
	{
		if($this->request->is('post'))
		{
			$requestData = $this->request->data;
			$requestData['Website']['create_time'] = date('Y-m-d h:i:s');
			if($this->Website->save($requestData))
			{
				$this->Session->setFlash('Your website(' . $requestData['Website']['title'] . ' has been saved.');
				$this->redirect(array('action' => 'index'));
			}
			else 
			{
				$this->Session->setFlash('Unable to add your website.');
			}
		}
	}
	
	public function view($id)
	{
		$this->Website->id = $id;
		$this->set('website', $this->Website->read());
	}
	
	public function edit($id)
	{
		$this->Website->id = $id;
		
		if($this->request->is('get'))
		{
			$this->request->data = $this->Website->read();
		}
		else
		{
			$requestData = $this->request->data;
			if($this->Website->save($requestData))
			{
				$this->Session->setFlash('Your website(' . $requestData['Website']['title'] . ') has been saved');
				$this->redirect(array('action' => 'index'));
			}
			else 
			{
				$this->Session->setFlash('Unable update your website');
			}
		}
	}
	
	public function download($fileName)
	{
		$ext = substr($fileName, strpos($fileName, '.') + 1);
		$name = substr($fileName, 0, strlen($fileName) - strlen($ext) - 1);
		$this->viewClass = 'Media';
		$params = array(
			'id' => $fileName,
			'name' => $name,
			'extension' => $ext,
			'download' => true,
			'path' => WEBROOT_DIR . DS . 'download' . DS
		);
		
		$this->set($params);
	}
}
?>